var prefix = jsctx + "/elecStation";

var tenantOperatorStation = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageTenantOperatorStation",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            elecStationId: $("#elecStationId").val().trim()
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '100',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'tenantCode', title: '运营商', width: '100', align: 'left'},
                        {field: 'operatorName', title: '分销商名称', width: '200', align: 'left'},
                        {field: 'loginName', title: '账号', width: '100', align: 'left'},
                        {field: 'contactName', title: '联系人', width: '100', align: 'left'},
                        {field: 'address', title: '公司地址', width: '200', align: 'left'},
                        {
                            field: 'status', title: '状态', width: '100',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span >正常</span>'
                                } else if (value == '1') {
                                    return '<span >冻结</span>'
                                }
                            }
                        },
                        {
                            title: '操作',
                            field: 'roleId',
                            align: 'center',
                            width: '100',
                            formatter: function (value, row, index) {

                                var a = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" title="删除" onclick="tenantOperatorStation.deleteTenantOperatorStation(\''
                                    + row.operatorStationId
                                    + '\')">删除</a> ';

                                return a;
                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function goBack() {
        window.history.back();
    }

    function addTenantOperatorStation() {
//  $("#recipient-name").val(sort)
        //  $("#ulId").val(ulId)
        $('#exampleModal').modal('show')

        $("#addTenantOperatorStation").unbind("click").bind("click", function () {

            var formData = new FormData();
            formData.append("loginName", $("#loginName").val());
            formData.append("elecStationId", $("#elecStationId").val());
            $.ajax({
                url: prefix + "/saveTenantOperatorStation",
                type: "post",
                data: formData,
                dataType: "json",
                processData: false, // 告诉jQuery不要去处理发送的数据，用于对data参数进行序列化处理 这里必须false
                contentType: false, // 告诉jQuery不要去设置Content-Type请求头
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        $('#exampleModal').modal('hide')
                        reLoad();
                    } else {
                        parent.layer.alert(data.message);
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        //  $('#exampleModal').modal('hide')
                    }

                }
            })
        })


    }

    function deleteTenantOperatorStation(operatorStationId) {
        layer.confirm('确定要删除选中的记录？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/deleteTenantOperatorStation",
                type: "post",
                data: {
                    'operatorStationId': operatorStationId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }
    
    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        addTenantOperatorStation: addTenantOperatorStation,
        deleteTenantOperatorStation:deleteTenantOperatorStation
    };
})();

var addTenantOperatorStation = (function () {
    function init() {
        validateRule();
    };

    function getCheckedRoles() {
        var adIds = "";
        $("input:checkbox[name=role]:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
        });
        return adIds;
    }

    var isajaxsend = true;

    function save() {
        if (isajaxsend) {
            var formData = new FormData($("#frmTenantOperatorStation")[0]);
            $("#roleIds").val(getCheckedRoles());
            $.ajax({
                cache: true,
                type: "POST",
                url: prefix + "/saveOrUpdateTenantOperatorStation",
                data: formData,
                processData: false,
                contentType: false,
                async: false,
                beforeSend: function () {
                    isajaxsend = false
                },
                error: function (request) {
                    parent.layer.alert("Connection error");
                },
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        window.history.back();
                    } else {
                        parent.layer.alert(data.message)
                    }

                },
                complete: function () {
                    isajaxsend = false
                }
            });
        }
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmTenantOperatorStation").validate({
            rules: {
                pileNo: {
                    required: true
                },
                pileName: {
                    required: true
                }

            },
            messages: {
                pileNo: {
                    required: icon + "请输入桩群编号"
                },
                pileName: {
                    required: icon + "请输入桩群名称"
                }
            },
            submitHandler: function (form) {
                save();
            }
        })
    }

    return {
        init: init
    };
})();
